<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <#list table.fields as field>
                    <el-form-item>
                        <el-input v-model="filters.${field.propertyName}" placeholder="${field.comment}"></el-input>
                    </el-form-item>
                </#list>
                <el-form-item>
                    <el-button type="primary" v-on:click="get${table.entityPath}s">查询</el-button>
                </el-form-item>
                <el-form-item style="float: right ;">
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="${table.entityPath}s" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <!--<el-table-column type="selection" width="55">
            </el-table-column>-->
            <el-table-column type="index" width="60">
            </el-table-column>

                <#list table.fields as field>
               <#if !field.keyFlag>
                <el-table-column prop="${field.propertyName}" label="${field.comment}" width="200" sortable>
                </el-table-column>
               </#if>
                </#list>



            <!-- <el-table-column prop="joinTime" label="加入时间"  width="200" sortable>
            </el-table-column> -->

            <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <!--<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>-->


            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="current"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="size"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    style="float:right;">
            </el-pagination>

        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="150px" :rules="editFormRules" ref="editForm">
                <#list table.fields as field>
                    <#if !field.keyFlag>
                        <el-form-item label="${field.comment}" prop="${field.propertyName}">
                            <el-input v-model="editForm.${field.propertyName}" auto-complete="off"></el-input>
                        </el-form-item>
                    </#if>

                </#list>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

                <!-- <el-form-item label="性别">
                    <el-radio-group v-model="addForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item> -->
                <#list table.fields as field>
                    <#if !field.keyFlag>
                       <el-form-item label="${field.comment}" prop="${field.propertyName}">
                             <el-input v-model="addForm.${field.propertyName}" auto-complete="off"></el-input>
                       </el-form-item>
                    </#if>
                </#list>


                <!-- 	<el-form-item label="联系方式" prop="repairPersonPhone">
                        <el-input v-model="addForm.repairPersonPhone" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="加入时间" prop="joinTime">
                        <el-date-picker type="date" placeholder="选择日期" v-model="addForm.joinTime"></el-date-picker>
                    </el-form-item> -->

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    import util from '../../common/js/util';
    //import NProgress from 'nprogress'
    import { get${table.entityName}List,add${table.entityName},edit${table.entityName},remove${table.entityName},uniqueValidateAdd,uniqueValidateUpdate
    <#list table.fields as field>
        <#if !field.keyFlag && field.name?contains("_id")>
            ,get${field.capitalName?replace("Id","")}List
        </#if>
    </#list>
    } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    nickname: '',
                    phoneNumber: '',
                    nameUser: ''
                },
                size:10,
                ${table.entityPath}s: [],
                tags:[],
                total: 0,
                current: 1,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    <#list table.fields as field>
                    ${field.propertyName}: [
                        { required: true, message: '请输入${field.comment}', trigger: 'blur' },
                        {min: 1, max: ${field.type},message: '${field.comment}长度1-${field.type}',trigger: 'change,blur'},

                    ],
                    </#list>
                },
                //编辑界面数据
                editForm: {
                    chargeTypeId: "",
                    chargeNumber: "",
                    chargeName: "",



                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                <#list table.fields as field>
                ${field.propertyName}: [
                    { required: true, message: '请输入${field.comment}', trigger: 'blur' },
                    {min: 1, max: ${field.type},message: '${field.comment}长度1-${field.type}',trigger: 'change,blur'},

                ],
                </#list>
                },
                //新增界面数据
                addForm: {
                    chargeTypeId: "",
                    chargeNumber: "",
                    chargeName: "",

                },
            <#list table.fields as field>
              <#if !field.keyFlag && field.name?contains("_id")>
            options${field.capitalName?replace("Id","")},
              </#if>
            </#list>

            }
        },
        methods: {
            //性别显示转换
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            formatStatus: function(row,column){
                return row.status ==0 ? '正常' :'冻结'
            },
            formatSubscribe : function(row,column){
                return row.subscribe ==0 ? '未关注' :'关注'
            },
            handleCurrentChange(val) {
                this.current = val;
                this.get${table.entityPath}s();
            },
            //每页显示数据量变更
            handleSizeChange: function(val) {

                this.size = val;
                this.get${table.entityPath}s();
            },


            //获取用户列表
            get${table.entityPath}s() {
                var _this=this;
                let para = {
                    current: _this.current,
                    size:_this.size,
                    <#list table.fields as field>
                    ${field.propertyName}:_this.filters.${field.propertyName},
                    </#list>
                };
                _this.listLoading = true;
                //NProgress.start();
                get${table.entityName}List(para).then((res) => {
                    if(res.code==0){
                    _this.total = res.data.total;
                    _this.${table.entityPath}s = res.data.records;

                    _this.listLoading = false;
                }else{

                }
                //NProgress.done();
            });
            },
        <#list table.fields as field>
            <#if !field.keyFlag && field.name?contains("_id")>
            get${field.propertyName?replace("Id","")}s() {
                var _this=this;
                let para = {
                    //current: _this.current,
                    size:-1
                };
                //_this.listLoading = true;
                //NProgress.start();
                get${field.capitalName?replace("Id","")}List(para).then((res) => {
                    if(res.code==0){
                    _this.options${field.capitalName?replace("Id","")} = res.data;

                    //_this.listLoading = false;
                }else{

                }
                //NProgress.done();
            });
            },
            </#if>
            </#list>
            //删除
            handleDel: function (index, row) {
                var _this=this;
                _this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    _this.listLoading = true;
                //NProgress.start();
                let para = { id: row.${table.entityPath}Id };
                remove${table.entityName}(para).then((res) => {
                    _this.listLoading = false;
                //NProgress.done();
                _this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                _this.get${table.entityPath}s();
            });
            }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                console.log(JSON.stringify(row))
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {

                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                        //NProgress.start();
                        let para = Object.assign({}, this.editForm);
                        para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                        edit${table.entityName}(para).then((res) => {
                            this.editLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        this.$refs['editForm'].resetFields();
                        this.editFormVisible = false;
                        this.get${table.entityPath}s();
                    });
                    });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                        //NProgress.start();
                        let para = Object.assign({}, this.addForm);
                        //para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                        add${table.entityName}(para).then((res) => {
                            this.addLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '提交成功',
                            type: 'success'
                        });
                        this.$refs['addForm'].resetFields();
                        this.addFormVisible = false;
                        this.get${table.entityPath}s();
                    });
                    });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                //NProgress.start();
                let para = { ids: ids };
                batchremove${table.entityName}(para).then((res) => {
                    this.listLoading = false;
                //NProgress.done();
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.get${table.entityPath}s();
            });
            }).catch(() => {

                });
            }
        },
        mounted() {
            this.get${table.entityPath}s();
        }
    }

</script>

